﻿using System;
using System.Collections.Generic;
using System.Text;
using ZLDC.Common.Estate;
using System.Data;

namespace ZLDC.Topo.BLL.Estate
{
    public class HousingAlertBLL
    {
                                                                                                                                                                public static string p_housingAlert_save(int Id, int housingId, string AlertType, string AlertScope, DateTime AlertDate,string AlertContent, int AddDeptId, string AddDept, int AddUserId, string AddUser, int dFlag, int dDeptId, string dDept, int dUserId, string dUser, int dltFlag) 
        {
            string outmsg = "";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                ZLDC.DAL.Estate.HousingAlertDAL.p_housingAlert_save(Id,housingId, AlertType, AlertScope,AlertDate,AlertContent,AddDeptId, AddDept, AddUserId, AddUser, dFlag, dDeptId, dDept, dUserId, dUser, dltFlag,out outmsg,out newid,db);
            }

            return outmsg;
        }
                                                                public static string p_housingAlert_list_table(int page, int pagesize, int housingId, int userid)
        {
            DataTable dt = null;
            int total = 0;                       int pageall = 0;         
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.HousingAlertDAL.p_housingAlert_list_table(page, pagesize, housingId,userid ,ref total, db);
            }

            pageall = _Utility.GetPageAll(pagesize, total);      
            _Json json = new _Json(dt, "yyyy-MM-dd HH:mm:ss");

            return "{page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";

        }
                                                                                                public static string p_housingAlert_update(int Id, int dFlag, int dDeptId, string dDept, int dUserId, string dUser)
        {
            int  retval = 0;
            string outmsg = "";
            DataTable dt = GetAlertList(Id);            string userid = "";
            string deptid="";
            string AlertScope = "";

            if (dt.Rows.Count > 0)
            {
                AlertScope = dt.Rows[0]["AlertScope"].ToString();
                userid = dt.Rows[0]["adduserid"].ToString();
                deptid=dt.Rows[0]["adddeptid"].ToString();
            }
            if (AlertScope == "2")             {
                if (deptid != dDeptId.ToString()) 
                {
                    return outmsg = "对不起，你没权限处理该提醒！";
                }
            }
            if (AlertScope == "1")             {
                if (userid != dUserId.ToString()) 
                {
                    return  outmsg="对不起，你没权限处理该提醒！";
                }
            }
            using (_Database db = new _Database("estate"))
            {
                ZLDC.DAL.Estate.HousingAlertDAL.p_housingAlert_update(Id,dFlag, dDeptId, dDept, dUserId, dUser, out retval, db);
            }
            if (retval > 0)
            {
                outmsg = "处理成功";
            }
            else 
            {
                outmsg = "处理失败";
            }
            return outmsg;
           
        }
        public static string p_AlertByUserid(int page, int pagesize, int userid)
        {
            DataTable dt = null;
            int total = 0;                       int pageall = 0;         
            int deptid = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID);
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.HousingAlertDAL.p_AlertByUserid(page, pagesize, userid,deptid, ref total, db);
            }

            pageall = _Utility.GetPageAll(pagesize, total);      
            _Json json = new _Json(dt, "yyyy-MM-dd HH:mm:ss");

            return "{page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";

        }
                public static string p_housingAlert_delete(int Id)
        {
            string outmsg = "";
            using (_Database db = new _Database("estate")) 
            {
                ZLDC.DAL.Estate.HousingAlertDAL.p_housingAlert_delete(Id, out outmsg, db);
            }
            return outmsg;
        }
        public static DataTable GetAlertList(int id)
        {
            DataTable dt = null;
            using (_Database db = new _Database("estate")) 
            {
              dt= ZLDC.DAL.Estate.HousingAlertDAL.GetAlertList(id, db);
            }
            return dt;
        }

    }
}
